package com.atguigu.tingshu.order.strategy.impl;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TrackOrderTradeStrategy implements OrderTradeStrategy {

    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient ;

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient ;

    @Autowired
    private RedisTemplate<String , String> redisTemplate ;
    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {

        // 从tradeVo对象中获取相关的参数
        Long itemId = tradeVo.getItemId();              // 用户所点击的声音的id
        Integer trackCount = tradeVo.getTrackCount();   // 购买的集数
        if(trackCount < 0) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR) ;
        }

        // 构建OrderInfoVo对象
        OrderInfoVo orderInfoVo = new OrderInfoVo();

        // 根据声音的id查询声音数据
        Result<TrackInfo> trackInfoResult = trackInfoFeignClient.getTrackInfo(itemId);
        TrackInfo trackInfo = trackInfoResult.getData();

        // 获取专辑的id
        Long albumId = trackInfo.getAlbumId();
        Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(albumId);
        AlbumInfo albumInfo = albumInfoResult.getData();

        // 构建相关金额数据, 声音购买不支持折扣
        BigDecimal price = albumInfo.getPrice();
        BigDecimal originalAmount = trackCount.intValue() > 0 ? price.multiply(new BigDecimal(trackCount)) : price ;
        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setDerateAmount(new BigDecimal("0.00"));
        orderInfoVo.setOrderAmount(originalAmount);

        // 构建订单明细，一个声音信息就是一个订单明细
        // 查询需要购买的声音的列表
        Result<List<TrackInfo>> paidTrackInfoListResult = trackInfoFeignClient.findPaidTrackInfoList(itemId, trackCount);
        List<TrackInfo> trackInfoList = paidTrackInfoListResult.getData();

        List<OrderDetailVo> orderDetailVoList = trackInfoList.stream().map(track -> {
            OrderDetailVo orderDetailVo = new OrderDetailVo();
            orderDetailVo.setItemId(track.getId());
            orderDetailVo.setItemName(track.getTrackTitle());
            orderDetailVo.setItemUrl(track.getCoverUrl());
            orderDetailVo.setItemPrice(albumInfo.getPrice());
            return orderDetailVo;
        }).collect(Collectors.toList());

        orderInfoVo.setOrderDetailVoList(orderDetailVoList);

        // 声音购买不支持折扣，因此没有减免明细数据
        orderInfoVo.setOrderDerateVoList(Lists.newArrayList());

        // 给OrderInfoVo设置其他的数据(交易编号、付款项目类型、支付方式、时间戳、签名)
        this.setOtherInfo(orderInfoVo , tradeVo,redisTemplate);

        return orderInfoVo;
    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_TRACK;
    }
}
